import 'package:management/model/visitor_model.dart';
import 'package:management/utils/log_utils.dart';
import 'package:sqflite/sqflite.dart';
///用户表名
String tableUser = "user";

///账号
String columnUserName = "name";

///密码
String columnUserPassWord = "psw";

///人脸云token
String columnUserFaceToken = "faceToken";

///管理员模型
class User {
  User(this.name, this.psw, {this.faceToken});

  String? name;
  String? psw;
  String? faceToken;

  Map<String, Object?> toMap() {
    var map = <String, Object?>{
      columnUserName: name,
      columnUserPassWord: psw,
      columnUserFaceToken: faceToken,
    };
    return map;
  }

  User.fromMap(Map<String, Object?> map) {
    name = map[columnUserName] as String?;
    psw = map[columnUserPassWord] as String?;
    faceToken = map[columnUserFaceToken] as String?;
  }

  ///登录
  ///[user] 从数据库查出的数据
  Future<LoginState> login(User user, {String? name, String? psw}) async {
    if (user != null) {
      MyLog.i("user: ${user.name} ${user.psw}");
      if (psw == user.psw) {
        return LoginState("登录成功", true);
      } else {
        return LoginState("密码错误", false);
      }
    } else {
      return LoginState("用户名错误", false);
    }
  }

  //注册
  register(String name, String psw) {}
}

class LoginState {
  String msg;
  bool isOk;

  LoginState(this.msg, this.isOk);
}

class UserProvider {
  Database? db;

  UserProvider._();

  static final UserProvider instance = UserProvider._();

  void init() async {
    MyLog.w("UserProvider init");
    await open();
    List map = await db!.query(tableUser,
        columns: [columnUserName],
        where: '$columnUserName =?',
        whereArgs: ["admin"]);
    if (map.isEmpty) {
      await db!.insert(tableUser, User("admin", "admin").toMap());
    }
    close();
  }

  Future<int> insert(User user) async {
    await open();
    int count = await db!.insert(tableUser, user.toMap());
    close();
    return count;
  }

  Future<User?> queryName(String name) async {
    await open();
    List maps = await db!.query(tableUser,
        columns: [columnUserName, columnUserPassWord, columnUserFaceToken],
        where: '$columnUserName = ?',
        whereArgs: [name]);

    if (maps.length > 0) {
      close();
      return User.fromMap(maps.first);
    }
    close();
    return null;
  }

  //int count = await database.rawUpdate(
  //   'UPDATE Test SET name = ?, value = ? WHERE name = ?',
  //   ['updated name', '9876', 'some name']);
  Future<int> update(User old, User newUser) async {
    await open();
    int count = await db!.update(tableUser, newUser.toMap(),
        where: '$columnUserName = ? and $columnUserPassWord = ? ',
        whereArgs: [old.name, old.psw]);
    close();
    return count;
  }

  Future<int> deleteByName(String name) async {
    await open();
    int count = await db!
        .delete(tableUser, where: '$columnUserName = ?', whereArgs: [name]);
    close();
    return count;
  }

  Future open() async {
    db = await openDatabase(dbPath!);
  }

  ///关闭数据库
  Future close() async => db!.close();
}
